﻿Imports System.Data.SqlClient

Public Class StartupParameter

    Private Const cConnectionTimeOutForTest As Integer = 5

    Private Const cDEFAULT_ConnectionTimeout As Integer = 60
    Private Const cDEFAULT_Servername As String = "."

    Private Const cCommandLineKey_DBServerName As String = "Server"
    Private Const cCommandLineKey_DBName As String = "Database"
    Private Const cCommandLineKey_DBConnectionTimeout As String = "TimeOut"

    Private Shared _CommandLineArgs As Dictionary(Of String, String)
    Private Shared ReadOnly Property CommandLineArgs() As Dictionary(Of String, String)
        Get
            Dim myCommandLineArgs As Dictionary(Of String, String) = _CommandLineArgs
            If myCommandLineArgs Is Nothing Then
                myCommandLineArgs = New Dictionary(Of String, String)
                For Each myArg As String In My.Application.CommandLineArgs
                    Dim myKeyValuePair As String() = myArg.Split("="c)
                    If myKeyValuePair.Length = 2 Then
                        Dim myKey As String = myKeyValuePair(0)
                        Dim myValue As String = myKeyValuePair(1)
                        If Not myCommandLineArgs.ContainsKey(myKey) Then
                            myCommandLineArgs.Add(myKey, myValue)
                        End If
                    ElseIf myKeyValuePair.Length = 1 Then
                        Dim myKey As String = myKeyValuePair(0)
                        If Not myCommandLineArgs.ContainsKey(myKey) Then
                            myCommandLineArgs.Add(myKey, String.Empty)
                        End If
                    Else
                        Console.WriteLine(String.Format("Wrong parameter syntax found {0}", myKeyValuePair))
                    End If
                Next
                _CommandLineArgs = myCommandLineArgs
            End If
            Return myCommandLineArgs
        End Get
    End Property

    Private Overloads Shared Function GetCommandLineValueIngeger(ByVal aKeyName As String) As Integer
        Return GetCommandLineValueIngeger(aKeyName:=aKeyName, aDefaultValue:=0)
    End Function
    Private Overloads Shared Function GetCommandLineValueIngeger(ByVal aKeyName As String, ByVal aDefaultValue As Integer) As Integer
        Try
            Dim myValue As String = GetCommandLineValueString(aKeyName)
            Return CInt(myValue)
        Catch ex As Exception
            Return aDefaultValue
        End Try
    End Function

    Private Overloads Shared Function GetCommandLineValueString(ByVal aKeyName As String) As String
        Return GetCommandLineValueString(aKeyName:=aKeyName, aDefaultValue:=String.Empty)
    End Function
    Private Overloads Shared Function GetCommandLineValueString(ByVal aKeyName As String, ByVal aDefaultValue As String) As String
        Dim myCommandLineArgs As Dictionary(Of String, String) = CommandLineArgs
        Dim myValue As String = String.Empty
        myCommandLineArgs.TryGetValue(aKeyName, myValue)
        If myValue Is Nothing Then myValue = aDefaultValue
        Return myValue
    End Function

    Private Shared ReadOnly Property DBServername() As String
        Get
            Dim myReturn As String = GetCommandLineValueString(cCommandLineKey_DBServerName, cDEFAULT_Servername)
            Return myReturn
        End Get
    End Property

    Private Shared ReadOnly Property DBName() As String
        Get
            Dim myReturn As String = GetCommandLineValueString(cCommandLineKey_DBName)
            If myReturn = String.Empty Then
                myReturn = ConsoleAnswers.GetText("You must enter a valid DBName")
                If String.IsNullOrEmpty(myReturn) Then ConsoleAnswers.WriteErrorMessageAndEndApplication("no DBName was entered")
            End If
            Return myReturn
        End Get
    End Property

    Private Shared ReadOnly Property DBServerConnectionTimeout() As Integer
        Get
            Dim myReturn As Integer = GetCommandLineValueIngeger(cCommandLineKey_DBConnectionTimeout, cDEFAULT_ConnectionTimeout)
            Return myReturn
        End Get
    End Property

    Private Shared ReadOnly Property DBConnectionString() As String
        Get

            Dim myConBuilder As New SqlClient.SqlConnectionStringBuilder

            With myConBuilder
                'always first init for a test
                .ConnectTimeout = cConnectionTimeOutForTest

                .DataSource = DBServername
                .InitialCatalog = DBName
                .IntegratedSecurity = True

                Dim myConnection As SqlConnection = Nothing
                Try
                    myConnection = New SqlConnection(.ConnectionString)
                    myConnection.Open()
                Catch ex As Exception
                    ConsoleAnswers.WriteErrorMessageAndEndApplication(ex.Message)
                Finally
                    myConnection.Close()
                End Try

                .ConnectTimeout = DBServerConnectionTimeout
            End With
            Return myConBuilder.ConnectionString
        End Get
    End Property

    Private Shared _DBConnection As SqlConnection
    Public Shared ReadOnly Property DBConnection() As SqlConnection
        Get
            Dim myConnection As SqlConnection = _DBConnection
            If myConnection Is Nothing Then
                myConnection = New SqlConnection(DBConnectionString)
            End If

            If Not myConnection.State = ConnectionState.Open Then
                Try
                    myConnection.Open()
                    _DBConnection = myConnection
                Catch ex As Exception
                    ConsoleAnswers.WriteErrorMessageAndEndApplication(ex.Message)
                End Try
            End If
            Return myConnection
        End Get
    End Property

End Class
